package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import com.iwhalecloud.bss.kite.cart.common.util.KiteCartStringUtils;
import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.entity.StaticAttrDO;
import com.ztesoft.bss.common.bo.UserInfo;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <Description> 是否总部直签客户 联动处理 是否总部收费 <br>
 *
 * @version 1.0<br>
 * @since R9.0<br>
 */
@Service
public class CheckGroupInfoHandler extends AbstractAttrHandler {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(CommonAttrVO commonAttr) {
        UserInfo userInfo = ContextUtil.getLoginInfo().getUserInfo();
        List<StaticAttrDO> attrDOs = commonAttr.getAttrValues();
        // 国际业务48只能选否，49只能选是
        if (KiteStringUtils.isNotEmpty(userInfo.getPostProvinceNbr())) {
            if (userInfo.getPostProvinceNbr().startsWith("48")) {
                attrDOs = attrDOs.stream().filter(attrDO -> KiteStringUtils.equalsAny(attrDO.getAttrValue(), "1_0000107", "0")).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(attrDOs)) {
                    StaticAttrDO attrDO = attrDOs.get(0);
                    commonAttr.setValue(attrDO.getAttrValue());
                    commonAttr.setValueDesc(attrDO.getAttrValueDesc());
                    commonAttr.setAttrValues(attrDOs);
                }

            } else if (userInfo.getPostProvinceNbr().startsWith("49")) {
                attrDOs = attrDOs.stream().filter(attrDO -> KiteStringUtils.equalsAny(attrDO.getAttrValue(), "1_0000106", "1")).collect(Collectors.toList());
                if (KiteListUtils.isNotEmpty(attrDOs)) {
                    StaticAttrDO attrDO = attrDOs.get(0);
                    commonAttr.setValue(attrDO.getAttrValue());
                    commonAttr.setValueDesc(attrDO.getAttrValueDesc());
                    commonAttr.setAttrValues(attrDOs);
                }
            }
        }


    }


    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        KiteAttr attr = querySceneInstService
            .getCommonAttrsByFieldName(sceneInstId, LocalKeyConsts.IS_LEADER_CHARGE, "113");
        if (Objects.isNull(attr)) {
            attr = querySceneInstService
                .getCommonAttrsByFieldName(sceneInstId, LocalKeyConsts.IS_LEADER_CHARGE_NEW, "113");
        }
        String attrId = attr.getAttrId();
        List<AttrValue> attrValues = AttrValueCache.get(attrId);
        List<KiteAttrValue> result = new ArrayList();

        KiteOfferInst offerInst = querySceneInstService.getMainOfferInst(sceneInstId);
        String offerId = offerInst.getOfferId();
        //当产品是“行业5G消息”，此属性默认为“是”，且不可编辑，界面上不显示 不使用联动
        if (!KiteStringUtils.equalsAny(offerId, "10004322", "50040896")) {
            if (KiteStringUtils.isEqual("0", value)) {
                for (int i = 0; i < attrValues.size(); i++) {
                    if (KiteStringUtils.equalsAny(attrValues.get(i).getAttrValue(), "1_0000107", "0")) {
                        KiteAttrValue kiteAttrValue = new KiteAttrValue();
                        KiteBeanUtils.copyProperties(attrValues.get(i), kiteAttrValue);
                        result.add(kiteAttrValue);
                        attr.setValue(attrValues.get(i).getAttrValue());

                        String enumDesc = attrValues.stream().filter(attrDO -> KiteStringUtils.equalsAny(attrDO.getAttrValue(), "1_0000107", "0")).findAny().map(AttrValue::getAttrValueName).orElse("否");
                        attr.setValueDesc(enumDesc);
                    }
                }
            } else {
                // 根据总部直签判断，为否则总部收费为否，为是则总部收费保留原来值
                for (int i = 0; i < attrValues.size(); i++) {
                    KiteAttrValue kiteAttrValue = new KiteAttrValue();
                    KiteBeanUtils.copyProperties(attrValues.get(i), kiteAttrValue);
                    result.add(kiteAttrValue);
                }
                // 订购，直签为是，则总部收费初值为否，变更，直签为是，总部收费保留原来的值
                if (KiteStringUtils.equalsAny(attr.getValue(), "1", "1_0000106")) {
                    String attrValue = attr.getValue();
                    String enumDesc = attrValues.stream().filter(attrDO -> KiteStringUtils.equalsAny(attrDO.getAttrValue(), attrValue)).findAny().map(AttrValue::getAttrValueName).orElse("是");
                    attr.setValueDesc(enumDesc);
                } else {
                    attr.setValue("1_0000107");
                    if (KiteStringUtils.isEqual(LocalKeyConsts.IS_LEADER_CHARGE_NEW, attr.getFieldName())) {
                        attr.setValue("0");
                    }
                    String enumDesc = attrValues.stream().filter(attrDO -> KiteStringUtils.equalsAny(attrDO.getAttrValue(), "1_0000107", "0")).findAny().map(AttrValue::getAttrValueName).orElse("否");
                    attr.setValueDesc(enumDesc);
                }
            }
            attr.setAttrValues(result);
            SpringUtil.getBean(IOperateSceneCommonService.class).setCommonAttr(KiteWebContext.getContext().getCurrentSceneInstId(), attr);
        }
        return super.apply(sceneInstId, fieldName, value, valueDesc);
    }

}
